package com.ddz.service;

import com.ddz.model.Poker;
import com.ddz.util.CommonUtil;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class PokerService {
    // 定义所有扑克牌的集合
    private List<Poker> allPokers = new ArrayList<>();
    // 玩家牌
    private Map<String,List<Poker>> playersPokers_map = new ConcurrentHashMap<>();

    public PokerService() {
        // 生成黑桃 (1), 红桃 (2), 梅花 (3), 方块 (4) 各13张牌
        for (int suit = 1; suit <= 4; suit++) {
            for (int number = 1; number <= 13; number++) {
                String pokerValue = suit + "-" + number;
                allPokers.add(new Poker(pokerValue));
            }
        }
        // 生成大小王 (5-1) 和 (5-2)
        allPokers.add(new Poker("5-1")); // 小王
        allPokers.add(new Poker("5-2")); // 大王

        // 打乱牌堆顺序
        Collections.shuffle(allPokers);
    }

    /**
     * 分发牌
     */
    public void dealPokers(List<String> userid_ls) {
        // 将每个玩家的牌集合加入到结果列表
        List<List<Poker>> playersPokers = new ArrayList<>();
        // 发地主牌
        List<Poker> loadPokers = new ArrayList<>();
        // 将牌分发给三个玩家
        List<Poker> player1Pokers = new ArrayList<>();
        List<Poker> player2Pokers = new ArrayList<>();
        List<Poker> player3Pokers = new ArrayList<>();
        // 轮流分牌给三个玩家
        for (int i = 0; i < allPokers.size(); i++) {
            Poker poker = allPokers.get(i);
            if (i < 3) {
                loadPokers.add(poker);
            } else {
                switch (i % 3) {
                    case 0:
                        player1Pokers.add(poker);
                        break;
                    case 1:
                        player2Pokers.add(poker);
                        break;
                    case 2:
                        player3Pokers.add(poker);
                        break;
                }
            }
        }
        // 对每个玩家的牌进行排序
        CommonUtil.pokerSort(loadPokers);
        CommonUtil.pokerSort(player1Pokers);
        CommonUtil.pokerSort(player2Pokers);
        CommonUtil.pokerSort(player3Pokers);

        playersPokers.add(player1Pokers);
        playersPokers.add(player2Pokers);
        playersPokers.add(player3Pokers);

        playersPokers_map.putIfAbsent("loadPokers", loadPokers);
        for (int i = 0;i<3;i++) {
            playersPokers_map.putIfAbsent(userid_ls.get(i), playersPokers.get(i));
        }
    }

    // 为玩家添加地主牌
    public void addloadPokers(String userid){
        // 合并两个列表并排序
        playersPokers_map.get(userid).addAll(playersPokers_map.get("loadPokers"));
        CommonUtil.pokerSort(playersPokers_map.get(userid));
    }

    // 获取指定玩家的扑克牌id列表
    public List<String> getAllPokers(String userid) {
        List<String> pokers = new ArrayList<>();
        for (Poker poker : playersPokers_map.get(userid)) {
            pokers.add(poker.getValue());
        }
        return pokers;
    }

    // 从手牌中删除指定牌
    public void delPokers(String userid,List<Poker> Pokers) {
        System.out.println(playersPokers_map);
        System.out.println(userid);
        System.out.println(playersPokers_map.get(userid));
        for (int i = 0;i<Pokers.size();i++) {
            for (int j = 0;j<playersPokers_map.get(userid).size();j++) {
                if (Pokers.get(i).getValue().equals(playersPokers_map.get(userid).get(j).getValue())) {
                    playersPokers_map.get(userid).remove(j);
                }
            }
        }
    }
}
